package com.huiminpay.transaction.service;

import com.alibaba.fastjson.JSON;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.exception.BusinessCast;
import com.huiminpay.common.cache.util.EncryptUtil;
import com.huiminpay.common.cache.util.ParseURLPairUtil;
import com.huiminpay.transaction.api.TransactionServiceApi;
import com.huiminpay.transaction.api.dto.PayOrderDTO;
import com.huiminpay.transaction.api.dto.QRCodeDto;
import com.huiminpay.transaction.mapper.AppPlatformChannelMapper;
import com.yh.merchant.api.AppServiceApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

/**
 * @Author: HWW
 * @Date: 2021/8/3111:15
 * @Desc:
 */
@Service
@Slf4j
public class TransactionService implements TransactionServiceApi {

    @Reference
    private AppServiceApi appServiceApi;

    @Value("${huiminpay.payurl}")
    private String payurl;



    @Autowired
    private AppPlatformChannelMapper appPlatformChannelMapper;

    @Override
    public String createStoreQRCode(QRCodeDto qrCodeDto) throws Exception {
        Boolean tmp = appServiceApi.checkStoreApp2Merchant(qrCodeDto.getAppId(),qrCodeDto.getStoreId(), qrCodeDto.getMerchantId());
        if(tmp){
            BusinessCast.cast(CommonErrorCode.E_200233);
        }

        PayOrderDTO payOrderDTO = new PayOrderDTO();
        payOrderDTO.setMerchantId(qrCodeDto.getMerchantId());
        payOrderDTO.setAppId(qrCodeDto.getAppId());
        payOrderDTO.setStoreId(qrCodeDto.getStoreId());
        payOrderDTO.setSubject(qrCodeDto.getSubject());//显示订单标题
        payOrderDTO.setChannel("huimin_c2b");//服务类型
        payOrderDTO.setBody(qrCodeDto.getBody());//订单内容

        String jsonString = JSON.toJSONString(payOrderDTO);
        log.info("transaction service createStoreQRCode,JsonString is {}",jsonString);

        // 将支付信息保存到票据中
        String ticket = EncryptUtil.encodeUTF8StringBase64(jsonString);
        //支付入口
        String payEntryUrl = payurl +ticket;
        log.info("transaction service createStoreQRCode,pay‐entry is {}",payEntryUrl);
        return payEntryUrl;

    }

}
